package com.xc.service.Impl;

import com.alibaba.fastjson.JSON;
import com.xc.dao.FloorMapper;
import com.xc.pojo.Floor;
import com.xc.service.FloorService;
import com.xc.utils.JedisPoolUtils;
import com.xc.utils.RedisKey;
import com.xc.vo.FloorVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class FloorServiceImpl implements FloorService {
    @Resource
    private FloorMapper floorMapper;

    /**
     * 查询楼层列表
     * @param floorVo
     * @return
     */
    public List<Floor> queryFloorList(FloorVo floorVo) {
        return floorMapper.queryFloorList(floorVo);
    }
    /**
     * 添加楼层
     * @param floor
     * @return
     */
    public int addFloor(Floor floor) {
        //删除Redis缓存中的数据
        JedisPoolUtils.getJedis().del(RedisKey.FLOOR_LIST);
        return floorMapper.addFloor(floor);
    }
    /**
     * 修改楼层
     * @param floor
     * @return
     */
    public int updateFloor(Floor floor) {
        //删除Redis缓存中的数据
        JedisPoolUtils.getJedis().del(RedisKey.FLOOR_LIST);
        return floorMapper.updateFloor(floor);
    }


    public String queryFloorListByRedis() {
        Jedis jedis = JedisPoolUtils.getJedis();
        //从Redis缓存中读取数据
        String floor_list = jedis.get(RedisKey.FLOOR_LIST);
        //判断缓存中是否存在数据
        if(StringUtils.isEmpty(floor_list)){
            //查询数据库
            List<Floor> floorList = floorMapper.queryFloorList(null);
            //将数据转换成JSON并保存到缓存中
            floor_list = jedis.set(RedisKey.FLOOR_LIST, JSON.toJSONString(floorList));
        }
        return floor_list;
    }
}
